package cn.edu.swpu.cins.u_chain.config;

import cn.edu.swpu.cins.u_chain.utils.JedisAdapter;
import cn.edu.swpu.cins.u_chain.utils.PasswordService;
import cn.edu.swpu.cins.u_chain.utils.RedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
@SuppressWarnings("SpringJavaAutowiringInspection")
public class TokenFilter extends OncePerRequestFilter {

    @Autowired
    private PasswordService passwordService;
    @Autowired
    private JedisAdapter jedisAdapter;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        //验证token
//        String token = request.getHeader("token");
//        String tel = request.getHeader("tel");
//        if (!request.getRequestURI().startsWith("/UChain/user/")){
//            if (token == null || tel == null) {
//                forbidden(response);
//            }
//            String key = RedisKey.getuChainLoginToken(tel);
//            if (!jedisAdapter.exists(key)) {
//                forbidden(response);
//            }
//            String Rtoken = jedisAdapter.get(key);
//            if (!passwordService.match(Rtoken,token)) {
//                forbidden(response);
//            }
//        }
        filterChain.doFilter(request, response);
    }

    private void forbidden(HttpServletResponse response) {
        response.setStatus(403);
    }
}
